package com.wf.day_0603.test5;

import java.util.HashMap;
import java.util.Map;

/**
 * @author mpk
 * @since 2024/6/3 17:41:50
 */

/*Map练习：
        输入一个字符串中，统计每个字符出现次数。*/
public class MapDemo {
    public static void main(String[] args) {


        String str = "abcabcabksdfvjdgjjdfkjcAA";


        Map<Character, Integer> aChar = countChar(str);

        aChar.forEach((key,value)->
            System.out.println( key+"出现了" + value+"次"));

    }

    private static Map<Character, Integer> countChar(String str) {

        HashMap<Character, Integer> map = new HashMap<>();

        for (int i = 0; i < str.length(); i++) {


            if(map.containsKey(str.charAt(i))) {

            map.put(str.charAt(i), map.get(str.charAt(i))+1);
            }
            else
            {
                map.put(str.charAt(i), 1);
            }
        }
        return map;

    }


}
